package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private AddressService addressService;

    /**
     * 添加操作
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        try {
            addressService.add(address);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
    }

    /**
     * 分页操作
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return addressService.findPage(queryPageBean);
    }

    /**
     * 上传图片
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();
            //获取后缀名
            int index = imgFile.getOriginalFilename().lastIndexOf(".");
            //分割文件名
            String extention = imgFile.getOriginalFilename().substring(index);
            //用UUID给图片起名
            String fileName = UUID.randomUUID().toString()+extention;
            //七牛云上传图片
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //将文件名暂存到redis
            resource.sadd(RedisConstant.ADDRESS_PIC_RESOURCES,fileName);
            //返回信息和文件名
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            //失败返回信息
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }finally {
            //释放资源
            if(resource!=null){
                resource.close();
            }
        }
    }

    /**
     * 编辑操作,数据回显
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Address address=addressService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,address);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 编辑操作
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address1){
        try {
            addressService.update(address1);
            return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    /**
     * 删除功能
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            addressService.delete(id);
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }
}
